package com.interview.ujiuye;


import java.util.HashMap;
import java.util.Map;

public class Demo1 {
    //1.暴力法:
    //通过双重遍历数组中所有元素的两两组合,当出现符合的和时返回两个元素的下标
    public static int[] twoSum1(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i+1; j <nums.length ; j++) {
                if(target-nums[i]==nums[j]){
                    return new int[]{i,j};
                }
            }
        }
        return null;
    }
    //2.哈希(更优解法)
    public static int[] twoSum2(int[] nums, int target){
        Map<Integer,Integer> map=new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int param=target-nums[i];//2 7
            if(map.containsKey(param)){
                return new int[]{map.get(param),i};
            }
            map.put(nums[i],i);
        }
        return null;
    }
    public static void main(String[] args) {
        int[]nums ={2, 7, 11, 15};
        int target = 9;
        //int[]indexCollection=twoSum1(nums,target);
        int[]indexCollection=twoSum2(nums,target);
        if(indexCollection!=null){
            for (int index : indexCollection) {
                System.out.print(index+" ");
            }
        }
    }
}
